using UnityEngine;
using System;
using System.Collections.Generic;

namespace JufGame.ExMethod
{
    public static class MyExMethod
    {
        public static bool ContainLayer(this LayerMask mask, int layer)
        {
            return ((1<<layer) & mask) != 0;
        }
        public static Vector3 GetMouseWorldPos(this Camera currentCM)
        {
            return currentCM.ScreenToWorldPoint(Input.mousePosition);
        }
        public static bool IsInView(this Camera currentCM, Vector3 worldPos)
        {
            Transform camTransform = currentCM.transform;
            Vector2 viewPos = currentCM.WorldToViewportPoint(worldPos);
            Vector3 dir = (worldPos - camTransform.position).normalized;
            float dot = Vector3.Dot(camTransform.forward, dir);//判断物体是否在相机前面
            return (dot > 0 && viewPos.x >= 0 && viewPos.x <= 1 && viewPos.y >= 0 && viewPos.y <= 1);
        }
        public static bool IsWithinRectangle(this Vector2 vec3, Vector2 startPos, Vector2 endPos)
        {
            if (startPos.x < endPos.x)
            {
                if (startPos.x >= vec3.x || endPos.x < vec3.x)
                    return false;
            }
            else
            {
                if (endPos.x >= vec3.x || startPos.x < vec3.x)
                    return false;
            }
            if (startPos.y < endPos.y)
            {
                if (startPos.y >= vec3.y || endPos.y < vec3.y)
                    return false;
            }
            else
            {
                if (endPos.y >= vec3.y || startPos.y < vec3.y)
                    return false;
            }
            return true;
        }
        public static bool IsWithinCube(this Vector3 vec3, Vector3 startPos, Vector3 endPos)
        {
            if (startPos.x < endPos.x)
            {
                if (startPos.x >= vec3.x || endPos.x < vec3.x)
                    return false;
            }
            else
            {
                if (endPos.x >= vec3.x || startPos.x < vec3.x)
                    return false;
            }
            if (startPos.y < endPos.y)
            {
                if (startPos.y >= vec3.y || endPos.y < vec3.y)
                    return false;
            }
            else
            {
                if (endPos.y >= vec3.y || startPos.y < vec3.y)
                    return false;
            }
            if (startPos.z < endPos.z)
            {
                if (startPos.z >= vec3.z || endPos.z < vec3.z)
                    return false;
            }
            else
            {
                if (endPos.z >= vec3.z || startPos.z < vec3.z)
                    return false;
            }
            return true;
        }
    }
}


